<%--
  Created by IntelliJ IDEA.
  User: huimao
  Date: 2020/6/18
  Time: 8:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css"/>
    <style type="text/css">
        .layui-table img {
            width: auto;
            height: auto;
            max-width: 100% !important;
            max-height: 100%;
        }
    </style>
</head>
<body>
<%--添加的form表单--%>
<form class="layui-form" style="display: none;padding: 10px 50px 10px 10px" id="addForm">
    <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">电话</label>
        <div class="layui-input-block">
            <input type="text" name="userTelphone" placeholder="请输入电话" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="userPassword" placeholder="请输入密码" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn picture">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <input type="hidden" name="userImage"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="userName" placeholder="请输入用户名" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-block">
            <input type="text" name="userNickname" placeholder="请输入昵称" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="userSex" value="男" title="男" checked>
            <input type="radio" name="userSex" value="女" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">个性签名</label>
        <div class="layui-input-block">
            <input type="text" name="userAutograph" placeholder="请输入个性签名" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">省</label>
        <div class="layui-input-block">
            <input type="text" name="userProvince" placeholder="请输入所在省" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">城市</label>
        <div class="layui-input-block">
            <input type="text" name="userCity" placeholder="请输入所在城市" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="userStatus" value="1" title="是" checked>
            <input type="radio" name="userStatus" value="0" title="否">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">日期</label>
        <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
            <input type="text" class="layui-input test" id="test1" name="userCreateDate">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>
<%--添加的form表单结束--%>

<%--修改的form表单--%>
<form class="layui-form" style="display: none;padding: 10px 50px 10px 10px" id="updateForm" lay-filter="111">
    <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <input type="hidden" name="userId">
    <div class="layui-form-item">
        <label class="layui-form-label">电话</label>
        <div class="layui-input-block">
            <input type="text" name="userTelphone" placeholder="请输入电话" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="userPassword" placeholder="请输入密码" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn picture">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <input type="hidden" name="userImage"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="userName" placeholder="请输入用户名" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-block">
            <input type="text" name="userNickname" placeholder="请输入昵称" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="userSex" value="男" title="男" checked>
            <input type="radio" name="userSex" value="女" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">个性签名</label>
        <div class="layui-input-block">
            <input type="text" name="userAutograph" placeholder="请输入个性签名" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">省</label>
        <div class="layui-input-block">
            <input type="text" name="userProvince" placeholder="请输入所在省" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">城市</label>
        <div class="layui-input-block">
            <input type="text" name="userCity" placeholder="请输入所在城市" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="userStatus" value="1" title="是" checked>
            <input type="radio" name="userStatus" value="0" title="否">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">日期</label>
        <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
            <input type="text" class="layui-input test" id="test2" name="userCreateDate">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="update">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>
<%--修改的form表单结束--%>
<%--搜索框开始--%>
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-md1">
            <%--添加按钮--%>
            <button type="button" class="layui-btn" id="addButton">添加</button>
            <%--添加按钮结束--%>
        </div>
        <div class="layui-col-md3">
            <label class="layui-form-label">用户名:</label>
            <div class="layui-input-block">
                <input type="text" id="username" placeholder="请输入用户名" autocomplete="off" class="layui-input"
                       lay-verify="required">
            </div>
        </div>
        <div class="layui-col-md3">
            <label class="layui-form-label">电话:</label>
            <div class="layui-input-block">
                <input type="text" id="tel" placeholder="请输入用户电话" autocomplete="off" class="layui-input"
                       lay-verify="required">
            </div>
        </div>
        <div class="layui-col-md1 layui-col-md-offset1">
            <%--搜索按钮--%>
            <button type="button" class="layui-btn" id="ssButton">搜索</button>
            <%--搜索按钮--%>
        </div>
    </div>
</div>
<%--搜索框结束--%>

<%--表格--%>
<table id="bannerTable" lay-filter="bannerTable" lay-size="lg"></table>
<%--表格结束--%>

<%--table表格中的操作列 文档中的工具栏--%>
<script type="text/html" id="bannerBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>
<%--table表格中的操作列结束--%>

<script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>
<%--引入jquery的代码引入--%>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.12.4.js"></script>
<script>
    layui.use(['form', 'layer', 'table', 'upload', 'laydate'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var layerIndex;

        /*初始化表格信息*/
        table.render({
            elem: '#bannerTable'
            , url: '${pageContext.request.contextPath}/user/selectAllByNameAndTelPage' //数据接口
            , page: true //开启分页
            , limit: 5
            , limits: [5, 10]
            , cols: [[ //表头
                {field: 'userId', title: 'ID', sort: true, fixed: 'left'}
                , {field: 'userTelphone', title: '手机号'}
                ,
                {
                    title: "照片", templet:
                        function (data) {
                            return "<img src='${pageContext.request.contextPath}" + data.userImage + "'/>";
                        }
                }
                , {field: 'userNickname', title: '昵称'}
                , {field: 'userName', title: '用户名'}
                , {field: 'userSex', title: '性别'}
                , {field: 'userAutograph', title: '个性签名'}
                , {
                    title: '籍贯', templet: function (d) {
                        return d.userProvince + '-' + d.userCity;
                    }
                }
                , {
                    title: '状态', templet: function (d) {
                        if (d.userStatus == 1) {
                            return '<input type="checkbox" name="switch" lay-skin="switch" checked="checked">';
                        } else {
                            return '<input type="checkbox" name="switch" lay-skin="switch">';
                        }
                    }
                }
                , {field: 'userCreateDate', title: '日期'}
                , {fixed: 'right', title: "操作", align: 'center', toolbar: '#bannerBar', fixed: 'right'} //这里的toolbar值是模板元素的选择器指向了另一个script标签，该script标签的id一定是barDemo
            ]]
        });
        /*初始化表格信息结束*/


        /*给表格的操作按钮绑定事件开始，每个按钮点击之后发生的事情*/
        table.on('tool(bannerTable)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'edit') {
                //显示弹出层
                layerIndex = layer.open({
                    type: 1,
                    title: "修改法师信息",
                    area: "500px",
                    content: $("#updateForm")
                })
                //执行ajax查询出对象 给弹出层的表单赋值
                $.ajax({
                    url: "${pageContext.request.contextPath}/user/selectById",
                    data: "userId=" + data.userId,
                    dataType: "json",
                    success: function (json) {
                        //利用layui的表单中给表单赋值方法
                        form.val("111", json);//给表单赋值
                    }
                })
            }


        })
        /*给表格的操作按钮绑定事件结束*/


        /*给添加按钮设置点击事件开始*/
        $("#addButton").click(function () {
            //显示弹出层
            layerIndex = layer.open({
                type: 1,
                title: "添加用户",
                area: "500px",
                content: $("#addForm")
            })
        })
        /*给添加按钮设置点击事件结束*/

        /*给搜索按钮添加点击事件开始*/
        $("#ssButton").click(function () {
            //获取两个input数据
            var username=$("#username").val();
            var tel =$("#tel").val();
            console.log(username);
            console.log(tel);
            //利用layui进行表格重载
            table.reload('bannerTable', {
                url: '${pageContext.request.contextPath}/user/selectAllByNameAndTelPage'
                ,where: {
                    "username":username,
                    "tel":tel
                } //设定异步数据接口的额外参数
            });
        })
        /*给搜索按钮添加点击事件结束*/



        /*执行一个laydate实例开始*/
        laydate.render({
            elem: '.test'
        });
        /*执行一个laydate实例结束*/

        /*上传开始*/
        var uploadInst = upload.render({
            elem: '.picture' //绑定元素
            , url: '${pageContext.request.contextPath}/util/upload' //上传接口
            , done: function (res) {
                //上传完毕回调
                $("[name=userImage]").val(res.src);
                console.log(res.src);
            }
        });
        /*上传结束*/

        /*给添加表单绑定提交事件开始*/
        form.on('submit(add)', function (data) {
            var formData = $("#addForm").serialize();//自动将表单中所有的需要传递的表单元素的值封装成我们需要的格式。key=value&key=value
            //发出ajax请求。
            $.ajax({
                url: "${pageContext.request.contextPath}/user/insertUser",
                data: formData,
                type: "post",
                success: function (data) {
                    //关闭掉添加图书的弹出层
                    layer.close(layerIndex);
                    //把表单里面的数据清除掉。
                    //把jquery对象转换为dom对象。form类型的dom对象有reset方法可以对表单进行重置
                    $("#addForm").get(0).reset();
                    //弹出层提示添加成功，还要关闭
                    layer.msg("添加成功！");
                    //对table进行重新加载
                    table.reload("bannerTable");
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        /*给添加表单绑定提交事件结束*/

        /*给编辑表单提交按钮绑定事件开始*/
        form.on('submit(update)', function (data) {
            var formData = $("#updateForm").serialize();//自动将表单中所有的需要传递的表单元素的值封装成我们需要的格式。key=value&key=value
            //发出ajax请求。
            $.ajax({
                url: "${pageContext.request.contextPath}/user/updateUser",
                data: data.field,
                type: "post",
                success: function (data) {
                    //关闭掉添加图书的弹出层
                    layer.close(layerIndex);
                    //把表单里面的数据清除掉。
                    //把jquery对象转换为dom对象。form类型的dom对象有reset方法可以对表单进行重置
                    $("#updateForm").get(0).reset();
                    //弹出层提示添加成功，还要关闭
                    layer.msg("修改成功！");
                    //对table进行重新加载
                    table.reload("bannerTable"/*, {
                        page: {
                            curr:  //重新从第  页开始
                        }
                    }*/);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        /*给编辑表单提交按钮绑定事件结束*/


    });
</script>

</body>
</html>
